package com.pet.system.service.impl;

import com.pet.common.utils.DateUtils;
import com.pet.system.domain.PetBrand;
import com.pet.system.domain.PetProduct;
import com.pet.system.mapper.PetBrandMapper;
import com.pet.system.mapper.PetProductMapper;
import com.pet.system.service.IPetBrandService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 品牌Service业务层处理
 *
 * @author pet
 * @date 2022-10-09
 */
@Service
public class PetBrandServiceImpl implements IPetBrandService {
    @Autowired
    private PetBrandMapper petBrandMapper;

    @Autowired
    private PetProductMapper petProductMapper;

    /**
     * 查询品牌
     *
     * @param id 品牌主键
     * @return 品牌
     */
    @Override
    public PetBrand selectPetBrandById(Long id) {
        return petBrandMapper.selectPetBrandById(id);
    }

    /**
     * 查询品牌列表
     *
     * @param petBrand 品牌
     * @return 品牌
     */
    @Override
    public List<PetBrand> selectPetBrandList(PetBrand petBrand) {
        return petBrandMapper.selectPetBrandList(petBrand);
    }

    /**
     * 新增品牌
     *
     * @param categoryList 品牌列表
     * @param brandName 品牌名称
     * @param userId userId
     * @return 结果
     */
    @Override
    public void insertPetBrand(List<String> categoryList, String brandName, Long userId) throws Exception {
        for (String category : categoryList) {
            PetBrand petBrand = new PetBrand();
            petBrand.setCategory(category);
            petBrand.setBrandName(brandName);
            petBrand.setUserId(userId);
            PetBrand petBrandSelect = petBrandMapper.selectPetBrandByEntity(petBrand);
            if(ObjectUtils.isNotEmpty(petBrandSelect)){
                throw new RuntimeException("品牌：" + brandName + "已存在，请勿重复添加！");
            }
            int i = petBrandMapper.insertPetBrand(petBrand);
            if(1 != i){
                throw new RuntimeException("数据库异常，请联系管理员。");
            }
        }
    }

    /**
     * 修改品牌
     *
     * @param petBrand 品牌
     * @return 结果
     */
    @Override
    public int updatePetBrand(PetBrand petBrand) {
        petBrand.setUpdateTime(DateUtils.getNowDate());
        return petBrandMapper.updatePetBrand(petBrand);
    }

    /**
     * 批量删除品牌
     *
     * @param ids 需要删除的品牌主键
     * @return 结果
     */
    @Override
    public int deletePetBrandByIds(Long[] ids) {
        return petBrandMapper.deletePetBrandByIds(ids);
    }

    /**
     * 删除品牌信息
     *
     * @param id 品牌主键
     * @return 结果
     */
    @Override
    public int deletePetBrandById(Long id) {
        PetBrand petBrand = petBrandMapper.selectPetBrandById(id);
        List<PetProduct> petProductList = petProductMapper.selectPetProductByBrandId(petBrand.getId());
        if (CollectionUtils.isNotEmpty(petProductList)) {
            //不为空
            return 0;
        }
        return petBrandMapper.deletePetBrandById(id);
    }

    @Override
    public List<String> selectPetBrandCategoryList(Long userId) {
        return petBrandMapper.selectPetBrandCategoryList(userId);
    }

    @Override
    public List<String> selectPetBrandCategoryBrandList(Long userId, String category) {
        return petBrandMapper.selectPetBrandCategoryBrandList(userId, category);
    }
}
